<?php
require 'includes/config.php';
require 'includes/auth.php';
require 'includes/sanitize.php';
$data = sanitize($_GET);
$uid=$_SESSION['uid'];
$accesstoken = $_SESSION['accesstoken'];

if(isset($data['username']) OR isset($data['userid'])){
	// Check if it's a friend request
	if($data['type'] == 1){
		// Make friend request with backend
		$request = new makeRequest($backend.'api.php','POST','method=messages&accesstoken='
		.$accesstoken.'&auid='.$uid.'&username='.$data['username'].'&type=1');
		$tmp = $request->makeRequest();
		$http_code = $tmp[1];
		// Check if it's successful
		$emsg = ($http_code==200) ? "Friend request sent!" : $http_code;
	}else if($data['type']==2){// Send league invite
		// Post the league invite to the backend
		$request = new makeRequest($backend.'api.php','POST','method=leagueinvite&accesstoken='
		.$accesstoken.'&userid='.$data['userid'].'&auid='.$uid.'&leagueid='.$data['leagueid']); // Not sure if this backend request works
		$tmp = $request->makeRequest();
		$http_code = $tmp[1];
		// Check if it's successful
		$emsg = ($http_code==200) ? "League invite sent!" : $http_code; // Notify the user what happend
	}else{// Assume it's a normal message
		// Send message to backend
		$request = new makeRequest($backend.'api.php','POST','method=messages&accesstoken='
		.$accesstoken.'&username='.$data['username'].'&type=0&subject='.$data['subject'].'&msg='.$data['msg'].'&auid='.$uid);
		$tmp = $request->makeRequest();
		$http_code = $tmp[1];
		// Check if it's successful
		$emsg = ($http_code==200) ? "Message sent!" : $http_code;
	}
}

if(isset($data['postid'])){
	// Get post with specified id
	$request = new makeRequest($backend.'api.php?method=messages&accesstoken='
	.$accesstoken.'&auid='.$uid.'&postid='.$data['postid'], 'GET');
	$tmp = $request->makeRequest();
	$rbody = $tmp[0];
	$http_code = $tmp[1];
	$obj = json_decode($rbody);
	// Check so there is no error
	if($http_code==200){
		// Resolve id to username
		$request = new makeRequest($backend.'api.php?method=user&id='.$obj[0]->{'from'}, 'GET');
		$tmp = $request->makeRequest();
		$rbody = $tmp[0];
		$http_code = $tmp[1];
		$obj2 = json_decode($rbody);
		$username = $obj2[0]->{'username'};
		// Check for successful request and create the message to show on page
		if($http_code == 200){
			$msg.= "<table border='1'>
			<th>From</th>
			<th>Subject</th>
			<th>Sent</th>
			<th>Unread</th><tr>
			<td>".$username."</td>
			<td>".$obj[0]->{'subject'}."</td>
			<td>".$obj[0]->{'timestamp'}."</td>
			<td>".$obj[0]->{'read'}."</td></tr>
			</table>";
			if ($obj[0]->{'type'}==1){ // Friend request body
				$msg.="<a href=friends.php?friend=".$obj[0]->{'from'}."&unikey=a>Accept friend request</a>";
			}else if($obj[0]->{'type'}==2){ // League invite body
				$msg.="<a href=leagues.php?joinleague=".$obj[0]->{'text'}.">Accept league invite</a>";
			}else{// Normal message body
				$msg.= "<table width='300'><tr><td>".$obj[0]->{'text'}."</td></tr></table>";
			}
		}
	}
}else{
	// Get all messages to current users userid
	$request = new makeRequest($backend.'api.php?method=messages&accesstoken='
	.$accesstoken.'&action=to&auid='.$uid, 'GET');
	$tmp = $request->makeRequest();
	$rbody = $tmp[0];
	$http_code = $tmp[1];
	$obj = json_decode($rbody);
	$msg = "<table border=1><th>From</th><th>Subject</th><th>Sent</th><th>Unread</th>";
	// Go through all messages
	foreach($obj as &$value){
		// Resolve from id to from username
		$request = new makeRequest($backend.'api.php?method=user&id='.$value->{'from'}, 'GET');
		$tmp = $request->makeRequest();
		$rbody = $tmp[0];
		$http_code = $tmp[1];
		$obj2 = json_decode($rbody);
		// Create message to show on webpage
		$msg.="<tr>
		<td>".$obj2[0]->{'username'}."</td>
		<td><a href=messages.php?postid=".$value->{'id'}.">".$value->{'subject'}."</a></td>
		<td>".$value->{'timestamp'}."</td>
		<td>".$value->{'read'}."</td></tr>";
	}
	$msg.="</table>";
}
require 'includes/header.php';
require 'includes/topmenu.php';
echo '<div id="kaka">';
require 'includes/sidemenu.php'
?>
<div id="content"><?php 
echo $emsg; // Error message, if any
echo "<br />";
echo $msg;
?>
<div id="postm">
<table border="1">
	<tr>
		<td>
		<form name="postmsg" method="get" action="<?php echo $PHP_SELF; ?>"><input
			type="hidden" name="type" value="0" /> To user: <input type="text"
			name="username" value="Username" />
		
		</td>
		<td>Subject: <input type="text" name="subject" value="subject" /></td>


		<tr>
			<td colspan=2><textarea rows=20 cols=100 name="msg">Write you message here</textarea></td>
		</tr>
		<tr>
			<td colspan=2 align=center><input type="submit" value="submit"
				name="submit" /></td>
		</tr>
		</form>

</table>
</div>
<?php
require 'includes/footer.php';
?>